package com.gitee.wsgalaxy.gnodroid;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by wsgalaxy on 3/15/18.
 */
public class GndMainActivity extends Activity {
    public GndMainService.MyBinder binder = null;       //用于与service通信

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gnd_main_activity);
        //启动服务
        startService(new Intent(this, GndMainService.class));

        //绑定activity和service
        boolean bind_status = bindService(new Intent(this, GndMainService.class),
                new ServiceConnection() {
                    @Override
                    public void onServiceConnected(ComponentName name, IBinder service) {
                        Log.d("gnd", "connect");
                        binder = (GndMainService.MyBinder) service;
                    }

                    @Override
                    public void onServiceDisconnected(ComponentName name) {
                        Log.d("gnd", "disconnect");
                    }
                }, 0);
        if (!bind_status) {
            Log.d("gnd", "bind fail");
        }

    }

    @Override
    public void onDestroy()
    {
        //解除与service的绑定
        if (binder != null) {
            binder.activity = null;
            binder = null;
        }
        super.onDestroy();
    }
}
